package myapp 
{
	import myapp.events.AppEvent;
	import myapp.view.MainView;

	import com.bumpslide.preloader.PreloadEvent;
	import com.bumpslide.tween.FTween;
	import com.bumpslide.ui.Applet;
	import com.bumpslide.ui.Box;

	import flash.events.Event;

	/**
	 * Application Root, the Document Class for 'main.fla' 
	 * 
	 * The root of your .FLA file should be nothing but a blank frame.
	 * Make this the document class for your FLA file.  It will initialize 
	 * the application framework and then attach the main view of your application.
	 * 
	 * This class should remain a reusable part of the app template, and
	 * should not be cluttered with app logic.
	 * 
	 * @see myapp.Controller
	 * @see myapp.view.MainView
	 * 
	 * @author David Knape
	 */
	public class AppRoot extends Applet 
	{

		public var background:Box;

		public var mainView:MainView;

		public var controller:Controller;

		
		override protected function addChildren():void 
		{
			super.addChildren();
					
			debugEnabled = false;

			background = new Box( 0xdddddd );
			background.alpha = 0;
			addChild(background);
		}

		
		/**
		 * Where we initialize our controller
		 * 
		 * If there is a preloader, we will be added to the stage once loading is complete.
		 * If we are true _root, then this event will dispatch right away.
		 */
		override protected function onAddedToStage(e:Event):void 
		{
			super.onAddedToStage(e);
			
			// configure stage proxy
			stageProxy.updateDelay = 0; 
			stageProxy.setBounds(500, 400, 1200, 1000);
			
			// init controller
			controller = new Controller();
						
			// show loading message using the preloader
			dispatchEvent(new PreloadEvent(PreloadEvent.MESSAGE, 'Loading Images...'));
			
			var startupEvent:AppEvent = new AppEvent(AppEvent.STARTUP, this, handleStartupComplete, handleStartupError);
			startupEvent.dispatch();
		}

		
		/**
		 * Create the main view and configure stage proxy
		 */
		public function handleStartupComplete():void 
		{						
			debug('initView()');
			
			// tell preloader that we're all done
			dispatchEvent(new PreloadEvent(PreloadEvent.STARTUP_COMPLETE));
			
			// attach the main view
			mainView = new MainView();
			mainView.visible = false;
			addChild(mainView);  
			
			// Fade in
			FTween.fadeIn( background );
			FTween.fadeIn( mainView, 500 );
			
			// size didn't change, so we need to force update
			updateNow();       
		}   

		
		public function handleStartupError( info:Object ):void 
		{
			dispatchEvent(new PreloadEvent(PreloadEvent.MESSAGE, 'Error Loading Images.'));
		}

		
		/**
		 * This gets called on stage resize since this class extends Applet
		 */
		override protected function draw():void 
		{
			if(!stage) return;
			
			debug('draw');
			
			// background should fill true stage dimensions and bleed over a bit 
			background.setSize(stage.stageWidth + 200, stage.stageHeight + 200);
								
			// size main view to stageProxy constraints and center if we're maxed out
			if(mainView) {
				mainView.x = Math.max(Math.round((stage.stageWidth - width) / 2), 0);
				mainView.setSize(width, height);	
			}									
			super.draw();
		}
	}
}
